﻿using ImperialServer.Common.Entities;
using ImperialUtilities.Exceptions;
using ImperialUtilities.Utilities;
using ImperialWebSite.Content.Resources;
using ImperialWebSite.Controllers;
using ImperialWebSite.Presenters.Specie.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ImperialWebSite.Presenters.Specie.Classes
{
    public class SpecieRegistryPresenter
    {
        private ISpecieRegistry _view;
        private SpecieController _controller;
        private EventViewer _logger;

        /// <summary>
        /// Contructor de la clase
        /// </summary>
        /// <param name="view">Interfaz que enlaza con la vista</param>
        public SpecieRegistryPresenter(ISpecieRegistry view)
        {
            _view = view;
            _controller = new SpecieController();
            _logger = new EventViewer("ImperialWebSite.Presenters.Specie.Classes");
        }

        /// <summary>
        /// Metodo que hace la carga de las especies existentes en la base de datos
        /// </summary>
        internal void LoadCurrentSpecies()
        {
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "LoadCurrentSpecies"));

                _view.SessionCurrentSpecies = _controller.SelectAllSpecies();

                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = ie.ImperialMessage;
            }
            catch (Exception e)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = Messages.failureTransactionMessage;
                _logger.WriteEventViewerLog("LoadCurrentSpecies", e.Message, true);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "LoadCurrentSpecies"));
            }
        }

        /// <summary>
        /// Metodo que realiza la insercion de una nueva especie
        /// </summary>
        internal void InsertNewSpecie()
        {
            SpecieEntity newSpecie = null;
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "InsertNewSpecie"));

                if (!_controller.SpecieAlreadyRegistered(_view.SpecieNameText, _view.SessionCurrentSpecies))
                {
                    newSpecie = _controller.CreateNewSpecie(_view.SpecieNameText);
                    if (_controller.InsertNewSpecie(newSpecie))
                    {
                        _view.SuccessfulTransaction = true;
                        _view.TransactionMessage = Messages.successfulTransactionMessage;
                    }
                }
                else
                {
                    _view.SuccessfulTransaction = false;
                    _view.TransactionMessage = Messages.duplicatedSpecieMessage;
                }

                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = ie.ImperialMessage;
            }
            catch (Exception e)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = Messages.failureTransactionMessage;
                _logger.WriteEventViewerLog("InsertNewSpecie", e.Message, true);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "InsertNewSpecie"));
            }
        }
    }
}